<template>
  <div class="jsbos-common-layout" v-loading="loading">
    <div class="jsbos-common-layout-center">
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <Form
          :fullName="this.fullName"
          :enCode="this.enCode"
          ref="Form"
          @close="closeForm"
          @saveConfig="saveToConfig"
        />
      </div>
    </div>
  </div>
  <!--  -->
</template>
<script>
import Form from "@/components/jsbos-message/Form.vue";
import { saveByKeyName, getConfigByKeyName } from "@/api/system/sysConfig";
import request from "@/utils/request";

export default {
  components: { Form },
  data() {
    return {
      loading: false,
      id: "",
      fullName: "日程消息模板",
      enCode: "scheduleMessage",
    };
  },
  created() {
    this.initData();
  },
  methods: {
    async initData() {
      console.log(111);
      // debugger;
      let res = await this.getMessageTemplateByEncode(this.enCode);
      if (res.hasOwnProperty("data")) {
        this.id = res.data.id;
      }
      this.loading = false;
      this.$nextTick(() => {
        this.$refs.Form.init(this.id);
      });
    },
    //根据code获取模板
    getMessageTemplateByEncode(enCode) {
      return request({
        url: "/api/system/MessageTemplate/getMessageTemplateByEncode/" + enCode,
        method: "get",
      });
    },
    closeForm(isRefresh) {
      this.initData();
    },
    async saveToConfig() {
      console.log(22);
      let id = "1";
      let res = await this.getMessageTemplateByEncode(this.enCode);
      if (res.hasOwnProperty("data")) {
        id = res.data.id;
      }
      let formMessage = {
        enCode: this.enCode,
        keyValue: id,
        keyName: "scheduleMessage",
        name: this.fullName,
        category: "Schedule",
      };
      console.log(333);
      saveByKeyName(formMessage).then((res) => {
        console.log(444);
        this.$message.success(res.msg);
      });
    },
  },
};
</script>
